package cn.edu.hnu.gpg.controller;

import cn.edu.hnu.gpg.api.error.NotFoundException;
import cn.edu.hnu.gpg.dto.MessageWithName;
import cn.edu.hnu.gpg.entity.Bulletin;
import cn.edu.hnu.gpg.entity.Message;
import cn.edu.hnu.gpg.entity.User;
import cn.edu.hnu.gpg.service.BulletinService;
import cn.edu.hnu.gpg.service.MessageService;
import cn.edu.hnu.gpg.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    private final UserService userService;
    private final BulletinService bulletinService;
    private final MessageService messageService;
    private final Logger logger = LogManager.getLogger(UserController.class);

    public enum InboxMode {
        U, // Unread
        R, // Read
        A  // All
    }

    @Autowired
    public UserController(UserService userService, BulletinService bulletinService, MessageService messageService) {
        this.userService = userService;
        this.bulletinService = bulletinService;
        this.messageService = messageService;
    }

    @RequestMapping("/")
    public String userIndex(Map<String,Object> model) {
        List<Bulletin> bulletinList = bulletinService.listBulletin();
        model.put("bulletins", bulletinList);
        return "jsp/index.jsp";
    }

    @RequestMapping(value = "/inbox")
    public String toInbox(HttpServletRequest request, Map<String, Object> model, @RequestParam InboxMode mode) throws NotFoundException {
        User user = ((User) request.getSession().getAttribute("user"));

        List<Message> messageList = messageService.readAllReceiveMessage(user.getUserId());

        // TODO: 优化设计与算法
        // 使用丑陋的方式给消息加上 name 字段
        model.put("messageList", messageService.message2MessageWithName(messageList));

        switch (mode) {
            case A:
                return "jsp/shared/inbox_all.jsp";
            case R:
                return "jsp/shared/inbox_read.jsp";
            case U:
                return "jsp/shared/inbox_unread.jsp";
        }

        throw new NotFoundException();
    }

    @RequestMapping("/login")
    public String userLogin() {
        return "jsp/login.jsp";
    }

    @RequestMapping("/logout")
    public String userLogout(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            try {
                session.invalidate();
            } catch (IllegalStateException e) {
                logger.warn(e.getMessage());
            }
        }
        return "redirect: /";
    }

    @RequestMapping("/reset")
    public String resetPassword(){
        return "jsp/reset.jsp";
    }
}
